$(function () {
    var start = '';
    var end = '';
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 14);
    map.enableScrollWheelZoom(true); //允许滚动缩放
    map.addControl(new BMap.NavigationControl()); //添加地图平移缩放控件 
    map.addControl(new BMap.GeolocationControl()); //定位
    /*定位当前城市*/
    function myFun(result) {
        var cityName = result.name;
        map.setCenter(cityName);
    }
    var myCity = new BMap.LocalCity();
    myCity.get(myFun);


    $('#distpicker').distpicker({
        autoSelect: false
    });

    /*登录功能*/
    $('#login-btn').click(function () {
        var dialog = $('#login-dialog');
        var username = dialog.find('#ld-username').val().trim();
        if (username == '') {
            alert('用户名为空');
            return;
        }
        var passwd = dialog.find('#ld-passwd').val();
        if (passwd == '') {
            alert('密码为空');
            return;
        }
        $.ajax({
            url: 'api/login',
            type: 'post',
            data: {
                username: username,
                passwd: passwd
            },
            dataType: 'json',
            success: function (data) {
                alert(data.status);
                if (data.status == '登录成功') {
                    window.location.href = 'userindex.html';
                }
            }
        })
        $('#login-dialog').modal('hide');
    });

    /*注册功能*/
    $('#regist-btn').click(function () {
        var dialog = $('#regist-dialog');
        var username = dialog.find('#rd-username').val().trim();
        var cprovince = dialog.find('#rd-province').val().trim();
        var ccity = dialog.find('#rd-city').val().trim();
        var cdistrict = dialog.find('#rd-district').val().trim();
        var caddr = dialog.find('#rd-addr').val().trim();
        if (username == '') {
            alert('公司名为空');
            return;
        }

        var passwd = dialog.find('#rd-passwd').val();
        if (passwd == '') {
            alert('密码为空');
            return;
        }
        var passwd1 = dialog.find('#rd-passwd1').val();
        if (passwd1 != passwd) {
            alert('两次密码不符');
            return;
        }
        if (cprovince == '' || ccity == '' || cdistrict == '' || caddr == '') {
            alert('地址为空');
            return;
        }
        $.ajax({
            url: 'api/regist',
            type: 'post',
            data: {
                username: username,
                passwd: passwd,
                cprovince: cprovince,
                cdistrict: cdistrict,
                ccity: ccity,
                caddr: caddr
            },
            dataType: 'json',
            success: function (data) {
                alert(data.status);
            }
        });
        $('#regist-dialog').modal('hide');
    });

    /*搜索功能*/
    $('#search-btn').click(function () {
        var oid = $("#inp").val().trim();
        if (oid == '') {
            alert("搜索内容不能为空！")
        } else {
            $("#detail2").modal('show');
            var dialog = $('#detail2');
            $.ajax({
                url: 'api/ordersearch?oid=' + oid,
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    start = data.sprovince + data.scity + data.sdistrict + data.saddr;
                    end = data.eprovince + data.ecity + data.edistrict + data.eaddr;
                    dialog.find('#detail-saddr').text(data.sprovince + data.scity + data.sdistrict + data.saddr);
                    dialog.find('#detail-stime').text(data.stime);
                    dialog.find('#detail-info').text(data.info);
                    dialog.find('#detail-eaddr').text(data.eprovince + data.ecity + data.edistrict + data.eaddr);
                    dialog.find('#detail-etime').text(data.etime);
                    dialog.find('#detail-zprice').text(data.zprice);
                    dialog.find('#detail-cname').text(data.cname);
                    dialog.find('#detail-ctel').text(data.ctel);
                    dialog.find('#detail-carnum').text(data.Carnum);
                    dialog.find('#detail-dtel').text(data.Dtel);
                }
            });
        }
    });

    /*规划路径*/
    $('#showRoute').click(function () {
        search(start, end);
    });

    function search(start, end) {
        map.clearOverlays();
        var driving = new BMap.DrivingRoute(map, {
            renderOptions: {
                map: map,
                autoViewport: true
            }
        });
        driving.search(start, end);
    }
});
